package com.flower.admin.system.controller;

import com.flower.admin.core.enums.ResultEnum;
import com.flower.admin.core.exception.ResultException;
import com.flower.admin.core.utils.ResultVoUtil;
import com.flower.admin.core.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController implements ErrorController {

    /**
     * 跳转到登录页面
     */
    @GetMapping("/login")
    public String toLogin(Model model) {
        model.addAttribute("isCaptcha", "false");
        return "/login";
    }


    /**
     * 实现登录
     */
    @PostMapping("/login")
    @ResponseBody
    public ResultVo login(String username, String password, String captcha, String rememberMe) {
        // 判断账号密码是否为空
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new ResultException(ResultEnum.USER_NAME_PWD_NULL);
        }

//        // 判断验证码是否正确
//        ProjectProperties properties = SpringContextUtil.getBean(ProjectProperties.class);
//        if (properties.isCaptchaOpen()) {
//            Session session = SecurityUtils.getSubject().getSession();
//            String code = (String) session.getAttribute("captcha");
//            if (StringUtils.isEmpty(captcha) || StringUtils.isEmpty(code)
//                    || !captcha.toUpperCase().equals(code.toUpperCase())) {
//                throw new ResultException(ResultEnum.USER_CAPTCHA_ERROR);
//            }
//            session.removeAttribute("captcha");
//        }
//
        // 1.获取Subject主体对象
        Subject subject = SecurityUtils.getSubject();

        // 2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        // 3.执行登录，进入自定义Realm类中
        try {
            // 判断是否自动登录
            if (rememberMe != null) {
                token.setRememberMe(true);
            } else {
                token.setRememberMe(false);
            }
            subject.login(token);

            // 判断是否拥有后台角色
//            User user = ShiroUtil.getSubject();
//            if (user.getIsRole().equals(UserIsRoleEnum.YES.getCode())) {
//                return ResultVoUtil.success("登录成功", new URL("/"));
//            } else {
//                return ResultVoUtil.error("您不是后台管理员！");
//            }
        } catch (AuthenticationException e) {
            return ResultVoUtil.error("用户名或密码错误");
        }
        return null;
    }

    @Override
    public String getErrorPath() {
        return null;
    }
}
